import http from '@/support/http'
import { Code } from '@/enum/app'
import Message from '@/support/message'
import { ref, watch } from 'vue'
import { isFunction } from '@/support/helper'
import { ElMessage, ElMessageBox } from 'element-plus'

export function useRevokes(confirm: string = '确认关闭吗') {
  const isDeleted = ref(false)

  const beforeDestroy = ref()

//   fetch list
  function revokes(path: string, id: string | number) {


    ElMessageBox.prompt('当开卡回文为客户拒收时才允许撤单。请手动输入"确认撤单",完成验证', '撤单', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      inputPattern: /^确认撤单$/,
      inputErrorMessage: '请输入"确认撤单"以完成撤单验证',
    }).then(({ value }) => {
          http
            .post(path + '/cancel',{open_message:value,id:id})
            .then(r => {
                if (r.data.code === Code.SUCCESS) {
                  if(r.data.data.code == 1){
                    Message.success('撤单成功')
                    isDeleted.value = true
                  }else{
                    Message.error(r.data.data.message)
                  }
                    
                } else {
                    Message.error(r.data.message)
                }
            })
        })
        .catch(() => {
        })
  }

  const deleted = (reset: Function) => {
    watch(isDeleted, function (value) {                                             
      if (value) {
        isDeleted.value = false
        reset()
      }
    })
  }

  return { revokes, deleted }
}
